package cn.youqu.happy.module.youqu.service.systemHobby;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.youqu.happy.module.youqu.controller.admin.systemHobby.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.systemHobby.SystemHobbyDO;
import cn.youqu.happy.framework.common.pojo.PageResult;

import cn.youqu.happy.module.youqu.convert.systemHobby.SystemHobbyConvert;
import cn.youqu.happy.module.youqu.dal.mysql.systemHobby.SystemHobbyMapper;

import static cn.youqu.happy.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.youqu.happy.module.youqu.enums.ErrorCodeConstants.*;

/**
 * 系统爱好 Service 实现类
 *
 * @author 王其岩 20112001045
 */
@Service
@Validated
public class SystemHobbyServiceImpl implements SystemHobbyService {

    @Resource
    private SystemHobbyMapper systemHobbyMapper;

    @Override
    public Integer createSystemHobby(SystemHobbyCreateReqVO createReqVO) {
        // 插入
        SystemHobbyDO systemHobby = SystemHobbyConvert.INSTANCE.convert(createReqVO);
        systemHobbyMapper.insert(systemHobby);
        // 返回
        return systemHobby.getId();
    }

    @Override
    public void updateSystemHobby(SystemHobbyUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSystemHobbyExists(updateReqVO.getId());
        // 更新
        SystemHobbyDO updateObj = SystemHobbyConvert.INSTANCE.convert(updateReqVO);
        systemHobbyMapper.updateById(updateObj);
    }

    @Override
    public void deleteSystemHobby(Integer id) {
        // 校验存在
        this.validateSystemHobbyExists(id);
        // 删除
        systemHobbyMapper.deleteById(id);
    }

    private void validateSystemHobbyExists(Integer id) {
        if (systemHobbyMapper.selectById(id) == null) {
            throw exception(SYSTEM_HOBBY_NOT_EXISTS);
        }
    }

    @Override
    public SystemHobbyDO getSystemHobby(Integer id) {
        return systemHobbyMapper.selectById(id);
    }

    @Override
    public List<SystemHobbyDO> getSystemHobbyList(Collection<Integer> ids) {
        return systemHobbyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SystemHobbyDO> getSystemHobbyPage(SystemHobbyPageReqVO pageReqVO) {
        return systemHobbyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SystemHobbyDO> getSystemHobbyList(SystemHobbyExportReqVO exportReqVO) {
        return systemHobbyMapper.selectList(exportReqVO);
    }

}
